package pe.com.trener.layer.model.persistence.daoimpl;

/**
 * @author:   Oscar Erick Candela Carbajal
 * @rol:	  Analista Programador Web Java
 * @proyecto: Portal Trener
 *
 */

import java.util.ArrayList;
import java.util.List;

import org.springframework.dao.support.DataAccessUtils;
import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import pe.com.trener.layer.model.persistence.dao.ClassBookDiarioDao;
import pe.com.trener.layer.model.pojo.ClassBookDiario;

public class ClassBookDiarioDaoImpl extends HibernateDaoSupport implements ClassBookDiarioDao {

	public void crear(ClassBookDiario objectModel) throws Exception {		
		try {
			getHibernateTemplate().save(objectModel);
		} catch(Exception e) {
			e.printStackTrace();
			throw new Exception(e);
		} finally {		
		} 			
	}
		
	
	public void modificar(ClassBookDiario objectModel) throws Exception {		
		try {
			getHibernateTemplate().update(objectModel);
		} catch(Exception e) {
			e.printStackTrace();
			throw new Exception(e);
		} finally {		
		} 			
	}
	
	@SuppressWarnings("unchecked")
	public ClassBookDiario buscarPorPk(Integer parametroPk) throws Exception {
		try {
	        List<ClassBookDiario> resultQuery = new ArrayList<ClassBookDiario>();
	        ClassBookDiario objectModel = new ClassBookDiario();
	        HibernateTemplate ht = getHibernateTemplate();

		 	Object[] values = new Object[1];
	        values[0] = parametroPk;	        
	        
	        String queryString = " from " +
								 " ClassBookDiario o " +
								 " where " +
								 " 	  (o.codigoClassBookDiario = ?)";
	        
	        resultQuery = ht.find(queryString, values);
	        
	        objectModel = (ClassBookDiario)DataAccessUtils.uniqueResult(resultQuery);
	        
	        return objectModel;
		} catch(Exception e) {
			e.printStackTrace();
			throw new Exception(e);
		} finally {		
		}           
	}	

	@SuppressWarnings("unchecked")
	public List<ClassBookDiario> buscarPorTexto(String parametroTexto) throws Exception {
		try {
	        List<ClassBookDiario> resultQuery = new ArrayList<ClassBookDiario>();
		 	HibernateTemplate ht = getHibernateTemplate();
	        
		 	Object[] values = new Object[2];
	        values[0] = parametroTexto;
	        values[1] = parametroTexto;
		 	
	        String queryString = " from " +
								 " ClassBookDiario o " +
								 " where " +
								 " 	  ((o.codigoClassBookDiario like '%' + ? + '%')" +
								 " or (o.descripcion like '%' + ? + '%')) " +
	        					 " and o.estado = 1 "+	        					 
	        					 " order by o.fecha asc";
	        
	        resultQuery = (List<ClassBookDiario>)ht.find(queryString, values);
	        
	        return resultQuery;
		} catch(Exception e) {
			e.printStackTrace();
			throw new Exception(e);
		} finally {		
		}           
	}	
	
}